<?php

namespace app\api\controller\user;

use app\api\controller\Controllers;
use app\api\service\user\Address as AddressService;
use app\api\model\user\Address as AddressModels;

class Address extends Controllers
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(AddressService $service)
    {
        return success($service->index());
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(AddressService $service)
    {
        $post = $this->request->only(['username','mobile','area','address','default'], 'post');
        $this->validate($post, 'app\api\validate\user\Address.create');
        return $service->create($post)?
            success():
            failed('添加失败');
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read(AddressModels $model)
    {
        return success($model);
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  AddressModels $model
     * @return \think\Response
     */
    public function edit(AddressModels $model)
    {
        return success($model);
    }

    /**
     * 保存更新的资源
     *
     * @param  AddressModels $model
     * @param  AddressService $service
     * @return \think\Response
     */
    public function update(AddressModels $model, AddressService $service)
    {
        $put = $this->request->only(['username','mobile','area','address','default'], 'put');
        $this->validate($put, 'app\api\validate\user\Address.update');
        return $service->save($model, $put)?
            success():
            failed('编辑失败');
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete(int $id, AddressService $service)
    {
        return $service->destory($id)?
            success():
            failed('删除失败');
    }

    /**
     * 获取收货地址详情
     * @param int $id
     * @return \think\Response
     */
    public function defaultAddress(int $id, AddressService $service)
    {
        return success($service->defaultAddress($id));
    }

}
